package org.learnredis.dataReaders;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.learnredis.university.StudyProfile;
import org.learnredis.university.University;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class UniversityReader {

    private static final Logger logger
            = LoggerFactory.getLogger(UniversityReader.class);

    private UniversityReader() {}

    public static ArrayList<University> fileRead() throws IOException {
        ArrayList universityList = new ArrayList<University>();

        try {
            String filePath = UniversityReader.class.getResource("/universityInfo.xlsx").getPath();

            logger.info(String.format("Get file in path: %s", filePath));

            FileInputStream file = new FileInputStream(new File(filePath));

            logger.info("File found");

            XSSFWorkbook workbook = new XSSFWorkbook(file);

            XSSFSheet studentPage = workbook.getSheetAt(0);

            logger.info("Read university page");

            XSSFSheet universityPage = workbook.getSheetAt(1);

            Iterator<Row> rowUniversityIterator = universityPage.iterator();

            if (rowUniversityIterator.hasNext()) rowUniversityIterator.next();

            while (rowUniversityIterator.hasNext()) {
                Iterator<Cell> cellIterator = rowUniversityIterator.next().cellIterator();

                University university = new University();

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();

                    int index = cell.getColumnIndex();

                    switch (index) {
                        case 0:
                            university.setId(cell.getStringCellValue());
                            break;
                        case 1:
                            university.setFullName(cell.getStringCellValue());
                            break;
                        case 2:
                            university.setShortName(cell.getStringCellValue());
                            break;
                        case 3:
                            university.setYearOfFoundation((int) cell.getNumericCellValue());
                            break;
                        case 4:
                            university.setMainProfile(StudyProfile.convert(cell.getStringCellValue()));
                            break;
                        default:
                            logger.debug("Cell University not value");
                    }
                }
                universityList.add(university);
            }

            logger.info("Finish read file");

        } catch (FileNotFoundException e) {
            logger.error("File not found");
        } catch (IOException e) {
            System.out.println("IO Exception: " + e.getMessage());
            logger.error("Error read file",e);
        }

        return  universityList;
    }
}
